import tool
from pygame.locals import *
from jiong import *

class Player(Jiong):
    __walk_speed = 2
    __jump_speed = -6 # Y-axis up
    __fall_speed = 0.2 # 50FPS = 0.02S
    
    # pos (x, y): > 0
    def __init__(self, pos):
        frame = tool.load_frame('player.bmp', (3, 1), (0, 0, 0))
        Jiong.__init__(self, frame[0], pos, frame)
        self.__g_speed = 0 # fall speed from gravity
        self.__air = False # there is no jump, jump, jump .....
    
    def update(self):
        self.__g_speed += self.__fall_speed # to imitate gravity   
        dx = 0
        dy = self.__g_speed
        for keyname in self._Jiong__action:
            if keyname == K_LEFT:
                dx -= self.__walk_speed
            elif keyname == K_RIGHT:
                dx += self.__walk_speed
            elif keyname == K_SPACE:
                if not self.__air:
                    self.__air = True
                    self.__g_speed = self.__jump_speed
        
        coll = self.move((dx, dy))
        if len(coll) != 0: self.__g_speed = 0
        for (spr, side) in coll:
            if side == Jiong.TOP:
                self.__air = False
                self.__g_speed = 0
        
        Jiong.update(self)

        
        
        
        
        
        
        
        
        
        
        
        
        
        